package com.yiren.dbaa.module.practice_set.line.model;


import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.LineHomeContract;
import com.yiren.dbaa.module.practice_set.site.bean.LineDataValBean;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/15 14:42
 * @description: 线路首页
 */
public class LineHomeModel implements LineHomeContract.Model {

    private Activity activity;
    private Gson gson;

    public LineHomeModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void getLineFromServer(String userid, String siteCode, boolean isPublic, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadLineByUserSiteCode("?userId=" + userid + "&siteCode=" + siteCode), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<LineInfoEntity>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<LineInfoEntity>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    if (testBaseBean.getData() != null && !isPublic) {
                        List<LineInfoEntity> lineInfoList = new ArrayList<>();
                        for (int i = 0; i < testBaseBean.getData().size(); i++) {
                            if (testBaseBean.getData().get(i).getState() == 1) {
                                lineInfoList.add(testBaseBean.getData().get(i));
                            }
                        }
                        callBack.success(lineInfoList);
                    } else {
                        callBack.success(testBaseBean.getData());
                    }
                } else {
                    callBack.fail("数据请求错误!");
                }
            }

            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });
    }

    @Override
    public List<LineInfoEntity> lineHomedataMerge(List<LineInfoEntity> localLines, List<LineInfoEntity> serverLines) {
        for (LineInfoEntity localLine : localLines) {
            boolean exist = false;
            for (int i = 0; i < serverLines.size(); i++) {
                LineInfoEntity serverLine = serverLines.get(i);
                //本地存在 以本地数据为准
                if (localLine.getId().equals(serverLine.getId())) {
                    if (localLine.getLineVer() > 0)
                        localLine.setLineVerLast(serverLine.getLineVer());
                    //判断版本是否可更新
                    if (localLine.getLineVer() < serverLine.getLineVer()) {
                        localLine.setUpdateDetails(serverLine.getUpdateDetails());
                        localLine.setUpdate(true);
                    }
                    //判断本地文件是否存在
                    if (localLine.getLineVer() > 0)
                        localLine.setDownLoad(true);
                    serverLines.set(i, localLine);
                    exist = true;
                    break;
                }
            }
            //新增
            if (!exist) {
                localLine.setDownLoad(true);
                serverLines.add(localLine);
            }
        }
        return serverLines;
    }

    @Override
    public void downLoadLinePointData(String name, float lineVer, String siteCode, int pUserId, int cUserId, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.downloadLinedata("?name=" + name + "&lineVer=" + lineVer +
                "&siteCode=" + siteCode + "&pUserId=" + pUserId + "&cUserId=" + cUserId), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<LineDataValBean>> testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean<List<LineDataValBean>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    if (testBaseBean.getData().size() == 0) {
                        callBack.success(new LineDataValBean());
                    } else {
                        callBack.success(testBaseBean.getData().get(0));
                    }
                } else {
                    callBack.fail("线路点位数据请求错误!");
                }
            }

            @Override
            public void getFail() {
                callBack.fail("线路点位数据请求失败!");
            }
        });


    }
}
